<!doctype html>
<html>
<head>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"/>
    <meta charset="UTF-8">
    <title>权限分配</title>

    {% include 'template/baseCss.volt' %}
    <link href="/css/vendor/menu-selection.less" rel="stylesheet/less" type="text/css">
    <script src="/js/less.min.js"></script>
</head>

<body>
<main>
    <div class="user-box">
        <h2 class="title">权限分配</h2>
        <ul id="user-list">

        </ul>
    </div>

    <div class="menu-box">
        <div class="src-menu-box">
            <h2 class="title">选择权限</h2>
            <div class="menu-table-wrap" id="src-menu-container">
            </div>
            <div class="save-btn button" onClick="clickSave()">保 存</div>
        </div>

        <div class="dest-menu-box">
            <h2 class="title">获得权限</h2>
            <div class="dash-frame"></div>
            <div class="menu-table-wrap" id="dest-menu-container">
                <!-- 目标菜单 -->
            </div>
        </div>
    </div>
</main>

<div style="display: none;">
    <div id="data-user-list">{{ userList }}</div>
    <div id="data-menu-list">{{ menuList }}</div>
    <div id="cur-user-id">{{ curUserId }}</div>
    <div id="unable-menu">{{ unableMenu }}</div>
</div>
</body>

{% include 'template/baseJs.volt' %}
<script src="/js/vendor/menu-selection.js"></script>
<script>


    var AuthProxy = {
        userList: false,
        menuList: false,
        curSelectUser: false,
        curUserId: false,
        unableUseMenu: false,
        init: function () {
            var text = $("#data-user-list").text();
            this.curUserId = $("#cur-user-id").text();
            this.userList = eval("(" + text + ")");
            this.unableUseMenu = $("#unable-menu").text();

            this.menuList = this.transferOrigin2ViewMenuData(eval("(" + $("#data-menu-list").text() + ")"));
            var list = new Array();
            for (var i = 0; i < this.userList.length; i++) {
                var item = this.userList[i];
                list.push({
                    "title": item["phone"],
                    "subTitle": item["nickname"],
                    "user_id": item["user_id"],
                    "user_auth_menu": item["user_auth_menu"]
                });
            }
            menuSelectionM.setUserList(list);

            menuSelectionM.userSelectedItemCallback = function (item) {
                console.log("选择了用户  " + item.title + "   " + item.subTitle);
                AuthProxy.curSelectUser = item;
                AuthProxy.generateAuthMenu(item.user_auth_menu);
            }
        },

        generateAuthMenu: function (authData) {
            var tempSrcMenu = new Object();
            $.extend(true, tempSrcMenu, this.menuList);
            tempSrcMenu.length = this.menuList.length;
            if (authData) {

                var dataArray = new Array();
                var splitArray = authData.split(",");
                for (var splitKey in splitArray) {
                    var key = splitArray[splitKey];
                    dataArray[key] = true;
                }
                var needUnable = false;
                if (this.curSelectUser["user_id"] == this.curUserId) {
                    needUnable = true;
                }
                this.foreachOriginMenu(tempSrcMenu, function (item) {
                    if (dataArray[item.menuUrl]) {
                        item["checked"] = true;
                    }
                    item["enable"] = true;
                    if (needUnable) {
                        if (item.menuUrl == AuthProxy.unableUseMenu) {
                            item["enable"] = false;
                        }
                    }

                });
            } else {
                var needUnable = false;
                if (this.curSelectUser["user_id"] == this.curUserId) {
                    needUnable = true;
                }
                this.foreachOriginMenu(tempSrcMenu, function (item) {
                    item["checked"] = true;
                    item["enable"] = true;
                    if (needUnable) {
                        if (item.menuUrl == AuthProxy.unableUseMenu) {
                            item["enable"] = false;
                        }
                    }
                });
            }
            this.bindMenuData(tempSrcMenu)
        },
        foreachOriginMenu: function (data, callBack) {
            for (var key in data) {
                var item = data[key];
                var children = item["children"];
                if (children) {
                    for (var subItemKey in children) {
                        callBack(children[subItemKey]);
                    }
                }
            }
        },
        transferOrigin2ViewMenuData: function (originData) {
            var srcMenu = new Array();
            for (var key in originData) {
                var newItem = new Object();
                var item = originData[key];
                $.extend(newItem, item);

                var srcChildren = new Array();
                for (var subKey in item["children"]) {
                    var subNewItem = new Object();
                    var subItem = item["children"][subKey];
                    $.extend(subNewItem, subItem);
                    srcChildren.push(subNewItem);
                }
                newItem["children"] = srcChildren;
                srcMenu.push(newItem);
            }
            return srcMenu;
        },
        bindMenuData: function (srcMenu) {
            menuSelectionM.setSrcMenu(srcMenu);
            // 更新源菜单后需要绑定一下事件
            menuSelectionM.bindCheckboxEvent();
            menuSelectionM.menuChangedConfirmMsg = "目标菜单已改变，请确定是否退出!!!";
        }
    }

    $(function () {
        AuthProxy.init();
    });

    function clickSave() {
        // 菜单保存成功后需要调用一下，用于同步目标菜单和源菜单
        var targetStr = "";
        var isAllSelect = true;
        AuthProxy.foreachOriginMenu(menuSelectionM.destMenu, function (item) {
            if (item["checked"]) {
                targetStr += item["menuUrl"] + ",";
            } else {
                isAllSelect = false;
            }
        });
        console.info(targetStr);
        var userId = AuthProxy.curSelectUser["user_id"];
        var param = new Object();
        param["user_id"] = userId;
        param["auth_menu"] = targetStr;
        if (isAllSelect) {
            param["all"] = true;
        }
        feimaPost("/subsystem/updateAuth", param, function (result) {
            if (result.status == 0) {
                AuthProxy.curSelectUser["user_auth_menu"] = targetStr;
                menuSelectionM.menuSaved();
                FAlert("更新成功");
            } else {
                FAlert("更新失败");
            }
        });


    }

</script>

</html>






